package com.stars.empoly.vmodel

import android.app.Activity
import android.app.Application
import android.content.Intent
import android.os.Bundle
import androidx.databinding.Bindable
import androidx.lifecycle.viewModelScope
import com.google.gson.Gson
import com.stars.empoly.BR
import com.stars.empoly.activity.ApplyActivity
import com.stars.empoly.utils.net.NetEngine
import com.trailer.base.base.BRepository
import com.trailer.base.base.BViewModel
import com.trailer.base.utils.UiSwitch
import kotlinx.coroutines.launch

class AddressVModel(application: Application) : BViewModel<BRepository>(application) {


    var id:String?=null
    var address:String?=null
    var latitude:String?=null
    var longitude:String?=null


    @Bindable
    open var name: String? = null
        set(value) {
            field = value
            notifyPropertyChanged(BR.name)
        }

    @Bindable
    open var phone: String? = null
        set(value) {
            field = value
            notifyPropertyChanged(BR.phone)
        }

    @Bindable
    open var detailsAddress: String? = null
        set(value) {
            field = value
            notifyPropertyChanged(BR.detailsAddress)
        }


    fun toSave(){

        if(name.isNullOrEmpty()){
            toast("请输入姓名")
            return
        }
        if(phone.isNullOrEmpty()){
            toast("请输入手机号")
            return
        }
        if(latitude.isNullOrEmpty()){
            toast("请选择服务地址")
            return
        }
        if(detailsAddress.isNullOrEmpty()){
            toast("请输入门牌号")
            return
        }

        viewModelScope.launch {
            var it=repository().lreq { NetEngine.service.memberAddressAdd(name,phone,address,longitude,latitude,detailsAddress) }
            if(it.ok(true)){

                val intent = Intent()
                (repository().context as Activity).setResult(Activity.RESULT_OK, intent)
                (repository().context as Activity).finish()

            }
        }

    }


}
